package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.MsslyyBook;
import com.example.pojo.MsslyyUser;
import com.example.pojo.MsslyyWishlist;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.MsslyyWishlistMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MsslyyWishlistService extends ServiceImpl<MsslyyWishlistMapper, MsslyyWishlist> {

    @Resource
    private MsslyyWishlistMapper msslyyWishlistMapper;
    @Resource
    private MsslyyBookService msslyyBookService;
    public List getBookListById(Long id){
        QueryWrapper<MsslyyWishlist> queryWrapper=new QueryWrapper<>();
        LambdaQueryWrapper<MsslyyWishlist> eq = queryWrapper.lambda().eq(MsslyyWishlist::getUserId, id);
        List<MsslyyWishlist> list = list(eq);
        List<MsslyyBook> books = msslyyBookService.list();
        List books2 =new ArrayList<>();
        books.forEach(e->{
            list.forEach(b->{
                if (e.getId()==b.getBookId()&&books2.indexOf(e)==-1){
                    e.setAddTime(b.getCreateTime());
                    books2.add(e);
                }
            });
        });

        return books2;
    }
    public int addWishList(Long bookId,Long userId){
        QueryWrapper<MsslyyWishlist> queryWrapper=new QueryWrapper<>();
        LambdaQueryWrapper<MsslyyWishlist> eq = queryWrapper.lambda().eq(MsslyyWishlist::getBookId, bookId).eq(MsslyyWishlist::getUserId, userId);
        List<MsslyyWishlist> list = list(eq);
        //没有收藏记录
        if (list.size()==0){
            MsslyyWishlist msslyyWishlist = new MsslyyWishlist();
            msslyyWishlist.setBookId(bookId);
            msslyyWishlist.setUserId(userId);
            save(msslyyWishlist);
            return 1;
        }else {
            //取消收藏
            remove(eq);
            return 0;
        }



    }


}
